/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-05-10 16:49:46
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-11 10:08:48
 */
import cmdbres from '@/data/cmdbres';
import hcpcmdb from '@/data/hcpcmdb';
import request from '@/utils/request';

export async function queryRegion() {
    try {
        let res = await request({
            ...hcpcmdb.queryRegions,
            params: {
              cloudCode: 'wocloud'
            },
        });
        return res?.regions??[];
    } catch (error) {
        return [];
    }
}
export async function queryAZ(regionId) {
    // isEnable=1 可用 0 禁用
    try {
        let res = await request({
            ...hcpcmdb.queryAvailableZones,
            params: {
                regionId,
                pageSize: 10,
                pageNumber: 1,
                isEnable: 1
            },
        });
        return res?.azs??[];
    } catch (error) {
        return [];
    }
}
// 查询全部宿主机，regionId、azCode
export async function queryHosts() {
    try {
        const res = await request({
            ...hcpcmdb.queryHosts,
            params: {
                pageSize: 0,
                pageNumber: 0,
                cloudCode: ['wocloud']
            },
        });
        return res?.hosts??[];
    } catch (error) {
        return [];
    }
}
// 查询宿主机下的云主机，宿主机名称+资源池ID，可确定唯一宿主机
export async function queryServer({module, az_code, region_id, host_name}) {
    console.log(module, az_code, region_id, host_name);
    if(!(module && module.moduleCode == "server" && az_code && region_id && host_name)) return;
    try {
        const res = await request({
            ...cmdbres.queryResourceList,
            data: {
                cmModuleId: module.cmModuleId,
                pageNumber:0,
                pageSize: 0,
                queryFilter:{
                    EQ: {
                        az_code,
                        region_id, 
                        host_name,
                        trait: "direct"
                    }
                },
                sort: {
                    cm_create_time: 'DESC'
                }
            },
        });
        return res.data;
    } catch (error) {
        return [];
    } 
};
// 查询所有宿主机的告警类别数量
export async function getHostAlarm() {
    try {
        const data = {
            "collection": "alarm_info_realtime",
            "equalCondition": {
                "alarmStatus": 1,
                "confirmStatus": 0
            },
            "notEqualCondition": {
                "alarmType": "capacity"
            },
            "inCondition": {
                "resourceCode": [
                    "2001"
                ]
            },
            "subQueryFields": {
                "resourceName": {
                    "existField": 1
                },
                "resourceCode": {
                    "existField": 1
                },
                "secondaryType": {
                    "existField": 2,
                    "ifNull": ""
                },
                "info_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "5-info"
                    },
                    "then": 1,
                    "else": 0
                },
                "emergency_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "1-emergency"
                    },
                    "then": 1,
                    "else": 0
                },
                "high_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "2-high"
                    },
                    "then": 1,
                    "else": 0
                },
                "mid_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "3-mid"
                    },
                    "then": 1,
                    "else": 0
                },
                "junior_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "4-junior"
                    },
                    "then": 1,
                    "else": 0
                }
            },
            "groupFields": [
                "resourceCode",
                "secondaryType"
            ],
            "returnFields": {
                "total_count": 0,
                "info_sum": 0,
                "emergency_sum": 0,
                "high_sum": 0,
                "mid_sum": 0,
                "junior_sum": 0
            }
        }
        
        const res = await request({
            ...hcpcmdb.queryAlarmInfoAgg,
            data,
        });
        let alarmHosts = {};
        if(res.code == 200 && res.monitor?.length) {
            res.monitor.map(item => alarmHosts[item.secondaryType] = item)
        }
        
        return alarmHosts;
    } catch (error) {
        return {};
    }
}
// 查询多个云主机的告警数量
export async function getServerAlarm(resourceUuid) {
    if(resourceUuid.length == 0) return;
    try {
        const data = {
            "collection": "alarm_info_realtime",
            "equalCondition": {
                "alarmStatus": 1,
                "confirmStatus": 0
            },
            "notEqualCondition": {
                "alarmType": "capacity"
            },
            "inCondition": {
                "resourceCode": [
                    "1001","1002","8211"
                ],
                resourceUuid
            },
            "subQueryFields": {
                "resourceName": {
                    "existField": 1
                },
                "resourceUuid":{
                    "existField": 2,
                    "ifNull":""
                },
                "info_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "5-info"
                    },
                    "then": 1,
                    "else": 0
                },
                "emergency_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "1-emergency"
                    },
                    "then": 1,
                    "else": 0
                },
                "high_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "2-high"
                    },
                    "then": 1,
                    "else": 0
                },
                "mid_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "3-mid"
                    },
                    "then": 1,
                    "else": 0
                },
                "junior_sum": {
                    "existField": 0,
                    "if": {
                        "alarmLvl": "4-junior"
                    },
                    "then": 1,
                    "else": 0
                }
            },
            "groupFields": [
                "resourceUuid"
            ],
            "returnFields": {
                "resourceUuid":"",
                "total_count": 0,
                "info_sum": 0,
                "emergency_sum": 0,
                "high_sum": 0,
                "mid_sum": 0,
                "junior_sum": 0
            }
        }
        
        const res = await request({
            ...hcpcmdb.queryAlarmInfoAgg,
            data,
        });
        let alarmServer = {};
        if(res.code == 200 && res.monitor?.length) {
            res.monitor.map(item => alarmServer[item.resourceUuid] = item)
        }
        
        return alarmServer;
    } catch (error) {
        return {};
    }
}

